layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //用户列表
    var tableIns = table.render({
        elem: '#userList',
        url : '/news/list',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "userListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'title', title: '新闻标题', minWidth:100, align:"center"},
            {field: 'newsTypeName', title: '新闻类型', minWidth:100, align:"center"},
            {field: 'source', title: '新闻来源', minWidth:200, align:'center',
                templet:function(d){
                    return '<a class="layui-blue" href="mailto:'+d.source+'">'+(d.source || '')+'</a>';
                }
            },
            {field: 'brief', title: '新闻简介', align:'center'},
            {field: 'isHot', title: '新闻状态',  align:'center',
                templet:function(d){
                    return d.isHot == "1" ? "热门新闻" : "普通新闻";
                }
            },
            {field: 'pic', title: '图片', align:'center'},
            {field: 'newsDetail', title: '新闻详情', align:'center',
                templet:function(d){
                    return (d.newsDetail || '');
                }
            },
            {field: 'remarks', title: '备注信息', align:'center',
                templet:function(d){
                    return (d.remarks || '');
                }
            },
            {field: 'createDate', title: '创建时间', align:'center',minWidth:150,sort:true,
                templet:function(d){
                    //处理时间
                    var date = new Date(d.createDate);
                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                }
            },
            {title: '操作', minWidth:175, templet:'#userListBar',fixed:"right",align:"center"}
        ]]
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            table.reload("newsListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加用户
    function addNews(edit){
        var index = layui.layer.open({
            title : "添加用户",
            type : 2,
            content : "newsAdd.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    //console.log(edit)
                    body.find(".id").val(edit.id);  //newsId
                    body.find(".title").val(edit.title);  //新闻标题
                    //body.find(".newsTypeId").val(edit.newsTypeId);
                    body.find(".newsTypeId option[value="+edit.newsTypeId+"]").prop("selected",true);
                    body.find(".source").val(edit.source);  //新闻来源
                    body.find(".brief").val(edit.brief);  //新闻简介
                    //console.log("aa"+edit.isHot);
                    body.find(".isHot input[value="+edit.isHot+"]").prop("checked","checked");  //热门与否
                    body.find("#uploadImage").attr("src",edit.pic);  //图片
                    body.find(".newsDetail").text(edit.newsDetail);
                    body.find(".remarks").val(edit.remarks);  //新闻简介
                   /* body.find(".userGrade").val(edit.userGrade);  //会员等级
                    body.find(".userStatus").val(edit.userStatus);    //用户状态
                    body.find(".userDesc").text(edit.userDesc);    //用户简介*/
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addNews_btn").click(function(){
        addNews();
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('userListTable'),
            data = checkStatus.data,
            newsId = [];
        if(data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].id);
            }
            alert(newsId);
            layer.confirm('确定删除选中的新闻？', {icon: 3, title: '提示信息'}, function (index) {
                 $.get("/news/deleteAll",{
                     ids : newsId  //将需要删除的newsId作为参数传入
                 },function(data){
                tableIns.reload();
                layer.close(index);
                 })
            })
        }else{
            layer.msg("请选择需要删除的新闻");
        }
    })

    //列表操作
    table.on('tool(userList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        //console.log(data);
        if(layEvent === 'edit'){ //编辑
            addNews(data);
        }else if(layEvent === 'usable'){ //启用禁用
            var _this = $(this),
                usableText = "是否确定禁用此用户？",
                btnText = "已禁用";
            if(_this.text()=="已禁用"){
                usableText = "是否确定启用此用户？",
                btnText = "已启用";
            }
            layer.confirm(usableText,{
                icon: 3,
                title:'系统提示',
                cancel : function(index){
                    layer.close(index);
                }
            },function(index){
                _this.text(btnText);
                layer.close(index);
            },function(index){
                layer.close(index);
            });
        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此新闻？',{icon:3, title:'提示信息'},function(index){
                 $.get("/news/delete",{
                     id : data.id  //将需要删除的id作为参数传入
                 },function(data){
                    tableIns.reload();
                    layer.close(index);
                 })
            });
        }
    });

});